﻿using System;
using System.Runtime.InteropServices;
using UnityEngine;

namespace Super
{
	public static class WindowUtil
	{
		// 导入Windows API
		[DllImport("user32.dll")]
		private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

		[DllImport("user32.dll")]
		private static extern bool SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

		[DllImport("user32.dll")]
		private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

		[DllImport("user32.dll")]
		private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

		//[DllImport("user32.dll")]
		//public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

		[DllImport("user32.dll")]
		private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

		[DllImport("user32.dll")]
		private static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte alpha, uint dwFlags);

		// 定义常量
		private const int GWL_EXSTYLE = -20;
		private const int WS_EX_LAYERED = 0x80000;

		private const int GWL_STYLE = -16;
		private const int WS_BORDER = 0x00800000;
		private const int WS_CAPTION = 0x00C00000;
		private const int WS_THICKFRAME = 0x00040000;
		private const uint LWA_COLORKEY = 0x00000001;


		private const int HWND_TOPMOST = -1;
		private const int HWND_TOPDEFAULT = 0;
		private const int SWP_NOMOVE = 0x0002;
		private const int SWP_NOSIZE = 0x0001;
		private const uint SWP_FRAMECHANGED = 0x0020;

		// 定义RECT结构体
		[StructLayout(LayoutKind.Sequential)]
		public struct RECT
		{
			public int Left;
			public int Top;
			public int Right;
			public int Bottom;
		}

		// 设置窗口样式和透明度
		public static void RemoveBorderAndSetBlackTransparent()
		{
			RemoveBorder();
			SetColorAlpha(new Color32(0, 0, 0, 0));
		}

		/// <summary>
		/// 去除游戏窗体边框
		/// </summary>
		public static void RemoveBorder()
		{
			IntPtr hWnd = FindWindow(null, Application.productName);
			GetWindowRect(hWnd, out RECT windowRect);

			int windowLong = GetWindowLong(hWnd, GWL_STYLE);
			SetWindowLong(hWnd, GWL_STYLE, windowLong & ~(WS_BORDER | WS_CAPTION | WS_THICKFRAME));

			SetWindowPos(hWnd, HWND_TOPDEFAULT, windowRect.Left, windowRect.Top, windowRect.Right - windowRect.Left, windowRect.Bottom - windowRect.Top, SWP_FRAMECHANGED);
		}

		/// <summary>
		/// 设置窗体某颜色的alpha值
		/// unity设置游戏背景的alpha无效，通过此方法可以修改游戏窗体某颜色的alpha值
		/// </summary>
		public static void SetColorAlpha(Color32 color)
		{

			IntPtr hWnd = FindWindow(null, Application.productName);
			int exWindowLong = GetWindowLong(hWnd, GWL_EXSTYLE);

			//WS_EX_LAYERED 透明像素无法交互
			//WS_EX_TRANSPARENT 无法发生交互
			SetWindowLong(hWnd, GWL_EXSTYLE, exWindowLong | WS_EX_LAYERED);

			// 将颜色转换为COLORREF格式
			uint crKey = (uint)((color.r << 16) | (color.g << 8) | color.b);

			SetLayeredWindowAttributes(hWnd, crKey, 0, LWA_COLORKEY);
		}

		/// <summary>
		/// 设置窗体是否最前
		/// </summary>
		/// <param name="isTop"></param>
		public static void SetWindowPriority(bool isTop)
		{
			IntPtr hWnd = FindWindow(null, Application.productName);
			int priority = isTop ? HWND_TOPMOST : HWND_TOPDEFAULT;

			SetWindowPos(hWnd, priority, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
		}
	}

}
